//
//  MainTabBarController.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/16.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // self.tabBar 是只读属性,无法直接更改
//        self.tabBar =  XHTabBar
        
        let tabBar = XHTabBar()
        //只能通过 KVC 修改
        self.setValue(tabBar, forKey: "tabBar")
        
        //定义闭包
        tabBar.composeIconButtonClosure = {[weak self] in
            
            print("按钮被点击了,弹控制器")
            
//            self.view.addSubview(ComposeView())
            let composeView = ComposeView()
            
            composeView.show(self!)
        }
        
        //可以直接设置 tabBar.tintColor 来更改文字的颜色
//        tabBar.tintColor = UIColor.orangeColor()
        
        // Do any additional setup after loading the view.
        
//        let Vc = HomeTableController()
//        
//        Vc.title = "首页"
//        
//        let imageName = "tabbar_home"
//        
//        Vc.tabBarItem.image = UIImage(named: imageName)
//        Vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        
//        addChildViewController(UINavigationController(rootViewController: Vc))
        
        addChildViewController(HomeTableController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(MessageTableController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(DiscoverTableController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(ProfileTableController(), imageName: "tabbar_profile", title: "我")
        
        //演示如果设计湿把图片与文字都切到图片上
//        addChildViewController(UIViewController(), imageName:"allproducts", title: "")
    }
    
    private func addChildViewController(Vc:UIViewController,imageName:String,title:String) {
        
        //设置控制器的 title ,等于设置了 UINavigationController 和 UITabBarController 的 title
        Vc.title = title
        
//        //若想单独设置 UITabBarController 的 title
//        Vc.tabBarItem.title = "tabBar"
//        
//        //若想单独设置 UINavigationController 的 title
//        Vc.navigationItem.title = "nav"
        
        Vc.tabBarItem.image = UIImage(named: imageName)
        //修改图片的渲染模式
        Vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置文字颜色
        Vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        
        //设置文字大小---粗体(只能在 Normal 状态下设置, Selected 状态下设置无效果),同时2次写 Selected,下面的 setTitleTextAttributes 会把上面的效果覆盖
//        Vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.boldSystemFontOfSize(20)], forState: UIControlState.Normal)
        
        //若美工把图片和文字合并在一张图片上,则会图片显示会向上移
        //对于 imageInsets 来说,上下左右4个方向 向内都是 > 0 的
        Vc.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)

        addChildViewController(UINavigationController(rootViewController: Vc))

    }

}
